package com.base.doublepoint.listnode;

public class RemoveNthFromEnd {
    /**
     * 19. 删除链表的倒数第 N 个结点.
     * 双指针法
     *
     * @param head
     * @param n
     * @return
     */
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(-1, head);
        //添加哑结点,不然无法删除头结点
        ListNode left = dummy, right = dummy;
        //注意
        while (n > 0) { //注意
            right = right.next;
            n--;
        }

        while (right.next != null) { //注意
            left = left.next;
            right = right.next;
        }
        left.next = left.next.next;
        return dummy.next;


    }
}
